from rest_framework import serializers
from .models import *


# 为文章序列化器服务的标签序列化器
class TagSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = TagOfArticle
        fields = ['id', 'tag_name']


# 文章序列化器
class ArticleSerializer(serializers.ModelSerializer):
    # 携带文章的标签
    article_tags = TagSimpleSerializer(many=True, read_only=True)

    class Meta:
        model = Article
        fields = '__all__'


# 列表序列化器
class TagOfArticleListSerializer(serializers.ModelSerializer):
    # 获得当前标签的文章数目，只读字段
    related_article_num = serializers.IntegerField(read_only=True)

    class Meta:
        model = TagOfArticle
        fields = '__all__'


# 详情序列化器
class TagOfArticleDetailSerializer(serializers.ModelSerializer):
    tag_articles = ArticleSerializer(many=True, read_only=True)

    class Meta:
        model = TagOfArticle
        fields = '__all__'
